package com.xiaotu.spider.baidu.pipeline;

import com.xiaotu.common.model.StarModel;
import com.xiaotu.common.model.StarStringModel;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.APipeline;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.io.File;


@Component("StarBaikeInfoPipeline")
public class StarBaikeInfoPipeline extends APipeline
{
    private static final String[] STAR_BAIKE_INFO = new String[]{"starid", "name", "alias","occupation", "sex", "birthday",
            "height","weight", "school", "brokeragefirm", "country","born","nation", "constellation", "bloodtype","deathday"};
    private static final String[] STAR_NOT_BAIKE_INFO = new String[]{"starId", "name"};


    private static final String[] STAR_CONVERTER_BAIKE_INFO = new String[]{AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRINGARRAY_CONVERT, AConverter.STRINGARRAY_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT
            ,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,
            AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT,AConverter.STRING_CONVERT};

    private static final String[] STAR_CONVERTER_NOT_BAIKE_INFO = new String[]{AConverter.STRING_CONVERT,AConverter.STRING_CONVERT};


    private static final boolean[] STAR_THROW_BAIKE_INFO = new boolean[]{true, true, true, true, true,true, true, true, true, true,
            true, true, true, true, true,true};
    private static final boolean[] STAR_THROW_NOT_BAIKE_INFO = new boolean[]{true, true};

    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {

        StarStringModel model = resultItems.get(StarStringModel.class.getName());
        if (model != null){
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.STAR_BAIKE_INFO_YYYYMMDD);
            cacheHandler.hSet(key, model.getStarid(), model);
        }

        StarModel models = resultItems.get(StarModel.class.getName());
        if (models != null){
            String key2 = DataRedisKeyUtils.getKey(Constants.DataRedisKey.STAR_BAIKE_NOT_INFO_YYYYMMDD);
            cacheHandler.hSet(key2, models.getStarId(), models);
        }


        String filePath = resultItems.get("filePath");
        StarStringModel starModel = resultItems.get(StarStringModel.class.getName());
        if (starModel != null){
            FileUtil.writeFile(filePath + File.separator + "starModel.csv", StringUtil.list2CSVLine
                    (ConverterFactory.convert(JSONObject.fromObject(starModel), STAR_BAIKE_INFO,
                            STAR_CONVERTER_BAIKE_INFO, STAR_THROW_BAIKE_INFO), SepratorUtil.SEP_PIPE) +
                    SepratorUtil.LINE_SEPRATOR);
        }

        StarModel starNotModel = resultItems.get(StarModel.class.getName());
        if (starNotModel != null){
            FileUtil.writeFile(filePath + File.separator + "starNotModel.csv", StringUtil.list2CSVLine
                    (ConverterFactory.convert(JSONObject.fromObject(starNotModel), STAR_NOT_BAIKE_INFO,
                            STAR_CONVERTER_NOT_BAIKE_INFO, STAR_THROW_NOT_BAIKE_INFO), SepratorUtil.SEP_PIPE) +
                    SepratorUtil.LINE_SEPRATOR);
        }

    }
}
